package com.ddm.service.impl.webImpl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddm.entity.webEntity.New;
import com.ddm.entity.webEntity.Regulation;
import com.ddm.mapper.NewMapper;
import com.ddm.service.webService.NewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewServiceImpl extends ServiceImpl<NewMapper, New> implements NewService {

    @Autowired
    private NewMapper newMapper;

    @Override
    public Boolean add(New news) {
        if (news == null) {
            return false;
        }
        // 检查新闻链接
        if (news.getUrl() == null || news.getUrl().trim().isEmpty()) {
            return false;
        }
        // 检查新闻内容

        // 检查新闻图片
        if (news.getImg() == null || news.getImg().trim().isEmpty()) {
            return false;
        }
        // 检查发布日期
        if (news.getPublicDate() == null) {
            return false;
        }
        // 检查发布者
        if (news.getPublisher() == null || news.getPublisher().trim().isEmpty()) {
            return false;
        }
        save(news);  // 执行保存
        New savednews = getById(news.getId());  // 通过ID查询数据
        return savednews != null;  // 存在即返回true
    }

    @Override
    public Boolean update(New news) {
        if (news == null || news.getId() == null) {
            return false;
        }
        return updateById(news);
    }

    @Override
    public Boolean delete(Integer id) {
        if (id == null || id <= 0) {
            return false;
        }
        return removeById(id);
    }

    @Override
    public New getNewById(Integer id) {
        return newMapper.selectById(id);
    }

    @Override
    public List<New> getAllNews() {
        QueryWrapper<New> queryWrapper = new QueryWrapper<>();
        return newMapper.selectList(queryWrapper);
    }


    @Override
    public IPage<New> getNewsByIds(List<Integer> ids, Page<New> page) {
        if (CollectionUtils.isEmpty(ids)) {
            return new Page<>(); // 返回空分页对象
        }
        // 创建查询条件
        QueryWrapper<New> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids); // 查询ID在列表中的记录
        // 执行分页查询
        return newMapper.selectPage(page, wrapper);
    }
    @Override
    public IPage<New> getNewByPage(Page<New> page, String key) {
        QueryWrapper<New> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<New> lambdaWrapper = wrapper.lambda();

        if (key != null && !key.isEmpty()) {
            // 使用OR连接多个字段的模糊查询
            lambdaWrapper
                    .like(New::getContent, key)
                    .or().like(New::getPublisher, key);
        }

        lambdaWrapper.orderByDesc(New::getPublicDate);
        return this.page(page, lambdaWrapper);
    }
}